package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.example.pojo.Deploy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.example.handler.InServerHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.util.concurrent.SynchronousQueue;

@Controller
public class sendMsgToClient {
	@PostMapping("/sendmsg/{address}")//发送配置信息
    @ResponseBody
	public String sendmsg(@PathVariable("address") String address,Deploy deploy) {

        Object person1 = JSONObject.toJSON(deploy);
        String address1="/"+address;
		InServerHandler.channelGroup.forEach(ch -> {
			if (address1.equals(ch.remoteAddress().toString())) {
				ch.writeAndFlush(person1.toString());
				System.err.println("已发送");
			} else
				System.err.println("不发送");
		});
		return "提交成功";
	}
    @PostMapping("/sendmsg1/{address}")//发送轮询配置信息
    @ResponseBody
    public String sendmsg1(@PathVariable("address") String address, String content) {
        String address1="/"+address;
        System.out.println(content);
        InServerHandler.channelGroup.forEach(ch -> {
            if (address1.equals(ch.remoteAddress().toString())) {
                ch.writeAndFlush(content);
                System.err.println("已发送");
            } else
                System.err.println("不发送");
        });
        return "modbus";
    }
}
